<?php

//power by wshoto.com
global $_W, $_GPC;
$openid = m('user')->getOpenid();

$member = m('member')->getMember($openid);

$cacheKey = $_W['uniacid'].'_record_statistics_o_all_'.$member['id'];
$orderStatistics = WsCache::getCache($cacheKey);

if(empty($orderStatistics)){
	$orderStatistics = $this->model->getCommissionRecordStatistics($openid, array( 'o_status_0', 'o_status_1', 'o_status_2', 'o_status_3' ));
	WsCache::setCache($cacheKey, $orderStatistics);
}

$member['ordercount0'] = 0;
$member['commissioncount'] = 0;

foreach ($orderStatistics as $statistic) {
	$member['ordercount0'] += intval($statistic['order_count']);
	$member['commissioncount'] += $statistic['c_money_sum'];
}

$member['commissioncount'] = number_format($member['commissioncount'], 2);

$orderLists = $this->model->getCommissionRecord($member['id'], array(0, 1, 2, 3), array(), 0, 0);

$orderids = array();
$orderInfo = array();

foreach($orderLists as $orderList){
	if(!empty($orderList['orderid'])){
		$orderids[] = $orderList['orderid'];
		$orderInfo[$orderList['orderid']]['commission'] += $orderList['c_money'];
		$orderInfo[$orderList['orderid']]['level'] = $orderList['c_rank'];
	}
}

$orderids = array_flip(array_flip($orderids));

if ($_W['isajax']) {
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;

	$status = intval($_GPC['status']) <= 3 ? intval($_GPC['status']) : '';

	if($status !== '' ){

		$orderids = array();
		$orderInfo = array();

		foreach($orderLists as $orderList){
			if(!empty($orderList['orderid']) && $orderList['o_status'] == $status){
				$orderids[] = $orderList['orderid'];
				$orderInfo[$orderList['orderid']]['commission'] += $orderList['c_money'];
				$orderInfo[$orderList['orderid']]['level'] = $orderList['c_rank'];
			}
		}

		$orderids = array_slice($orderids, ($pindex - 1) * $psize, $psize);
	}


	$list = array();

	if (!empty($orderids)) {

		$list = m('order')->getOrderInfo($orderids, array('id', 'ordersn', 'createtime', 'status'));

		foreach ($list as &$row) {
			$row['commission'] = number_format($orderInfo[$row['id']]['commission'], 2);
			$row['createtime'] = date('Y-m-d H:i', $row['createtime']);
			if ($row['status'] == 0) {
				$row['status'] = '待付款';
			} else {
				if ($row['status'] == 1) {
					$row['status'] = '已付款';
				} else {
					if ($row['status'] == 2) {
						$row['status'] = '待收货';
					} else {
						if ($row['status'] == 3) {
							$row['status'] = '已完成';
						}
					}
				}
			}
			if ($orderInfo[$row['id']]['level'] == 1) {
				$row['level'] = '一';
			} else {
				if ($orderInfo[$row['id']]['level'] == 2) {
					$row['level'] = '二';
				} else {
					if ($orderInfo[$row['id']]['level'] == 3) {
						$row['level'] = '三';
					}
				}
			}
		}
		unset($row);
	}

	show_json(1, array('list' => $list, 'pagesize' => $psize,'debug'=>$orders));
}
include $this->template('order');